<?php

	include_once 'json_function.php';
	$ini_array = parse_ini_file("config/config.ini");
	
	$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the mysql db");
	mysqli_set_charset($conn, "utf8");
	ini_set('default_charset', "UTF-8");
	
	$id = $_POST["id"];
	$firstName = $_POST["firstName"];
	$surname = $_POST["surname"];
	$telephone = $_POST["telephone"];
	
	
	
	
	$getPatientMobileSql = "select patient_no, first_name, surname, mobile from patient ";
	$getDoctorMobileSql = "select doctor_no, doctor_first_name, doctor_surname, doctor_telephone_no from doctor";
	
	$hasParameter = false;
	
	if( strcmp(trim($id), "" ) != 0 ){
		if( $hasParameter == false ){
			$getPatientMobileSql .= " where ";
			$getDoctorMobileSql .= " where ";
		}else{
			$getPatientMobileSql .= " and ";
			$getDoctorMobileSql .= " and ";
		}
		$getPatientMobileSql .= " patient_no=".$id;
		$getDoctorMobileSql .= " doctor_no=".$id;
		$hasParameter = true;
	}
	
	if( strcmp(trim($firstName), "" ) != 0 ){
		if( $hasParameter == false ){
			$getPatientMobileSql .= " where ";
			$getDoctorMobileSql .= " where ";
		}else{
			$getPatientMobileSql .= " and ";
			$getDoctorMobileSql .= " and ";
		}
		
		$getPatientMobileSql .= " first_name like '%".$firstName."%' ";
		$getDoctorMobileSql .= " doctor_first_name like '%".$firstName."%' ";
		$hasParameter = true;
	}
	
	if( strcmp(trim($surname), "" ) != 0 ){
		if( $hasParameter == false ){
			$getPatientMobileSql .= " where ";
			$getDoctorMobileSql .= " where ";
		}else{
			$getPatientMobileSql .= " and ";
			$getDoctorMobileSql .= " and ";
		}
		$getPatientMobileSql .= " surname like '%".$surname."%' ";
		$getDoctorMobileSql .= " doctor_surname like '%".$surname."%' ";
		$hasParameter = true;
	}
	
	if( strcmp(trim($telephone), "" ) != 0 ){
		if( $hasParameter == false ){
			$getPatientMobileSql .= " where ";
			$getDoctorMobileSql .= " where ";
		}else{
			$getPatientMobileSql .= " and ";
			$getDoctorMobileSql .= " and ";
		}
		$getPatientMobileSql .= " mobile like '%".$telephone."%' ";
		$getDoctorMobileSql .= " doctor_telephone_no like '%".$telephone."%' ";
		$hasParameter = true;
	}
	
	
	
	$data = "{\"data\":[";
	
	$count = 0;
	
	$patientResult = mysqli_query($conn, $getPatientMobileSql);
	while( $row = mysqli_fetch_array($patientResult)){

		
		if( $count != 0 ){
			$data .= ",";
		}else{
			$count = 1;
		}
		
		$data .= "{\"No\":\"".$row["patient_no"]."\",";
		$data .= "\"firstName\":\"".replaceQuoteAndSlash($row["first_name"])."\",";
		$data .= "\"surname\":\"".replaceQuoteAndSlash($row["surname"])."\",";
		$data .= "\"mobile\":\"".$row["mobile"]."\",";
		$data .= "\"type\":\"patient\"}";
		
	}

	

	
	$doctorResult = mysqli_query($conn, $getDoctorMobileSql);
	echo mysqli_error($conn);
	while($row = mysqli_fetch_array($doctorResult)){

		
		if( $count == 0 ){
			$count = 1;
		}else{
			$data .= ",";
		}
		
		$data .= "{\"No\":\"".$row["doctor_no"]."\",";
		$data .= "\"firstName\":\"".replaceQuoteAndSlash($row["doctor_first_name"])."\",";
		$data .= "\"surname\":\"".replaceQuoteAndSlash($row["doctor_surname"])."\",";
		$data .= "\"mobile\":\"".$row["doctor_telephone_no"]."\",";
		$data .= "\"type\":\"doctor\"}";
		
	}
	
	$data .= "]}";
	
	echo JsonCharFilter($data);	

?>
